﻿using System;
using System.Collections.Generic;
using System.Text;
using Syberia.Metacode;
using System.Reflection;
using System.ComponentModel;

namespace Syberia.Client.Extensions.Metacode
{
    public class ValueEditorWrapper:ExplorableObject
    {
        public ValueEditorWrapper(object target,PropertyInfo tool):this(target,tool,null)
        {
            
        }
        public ValueEditorWrapper(object target, FieldInfo tool)
            : this(target, null, tool)
        {

        }
        public ValueEditorWrapper(object t, PropertyInfo t1, FieldInfo t2)
        {
            _target = t;
            _tool = t1;
            _toolf = t2;
        }

        object _target { get; set; }
        PropertyInfo _tool { get; set; }
        FieldInfo _toolf { get; set; }

        
        public string Value
        {
            get
            {
                try
                {
                    if (_tool != null) return _tool.GetValue(_target, null).ToString();
                    if (_toolf != null) return _toolf.GetValue(_target).ToString();
                    return null;
                }
                catch(Exception e)
                {
                    return e.ToString();
                }
            }
            set
            {
                Type t = null;
                if (_tool != null) t = _tool.PropertyType;
                if (_toolf != null) t = _toolf.FieldType;
                if (t == typeof(int))
                {
                    if (_tool != null) _tool.SetValue(_target,int.Parse(value), null);
                    if (_toolf != null) _toolf.SetValue(_target, int.Parse(value));
                }
                if (t == typeof(float))
                {
                    if (_tool != null) _tool.SetValue(_target, float.Parse(value), null);
                    if (_toolf != null) _toolf.SetValue(_target, float.Parse(value));
                }
                if (t == typeof(double))
                {
                    if (_tool != null) _tool.SetValue(_target, double.Parse(value), null);
                    if (_toolf != null) _toolf.SetValue(_target, double.Parse(value));
                }
                if (t == typeof(string))
                {
                    if (_tool != null) _tool.SetValue(_target, (value), null);
                    if (_toolf != null) _toolf.SetValue(_target, (value));
                }
                if (t == typeof(bool))
                {
                    if (_tool != null) _tool.SetValue(_target, bool.Parse(value), null);
                    if (_toolf != null) _toolf.SetValue(_target, bool.Parse(value));
                }
                if (t.IsEnum)
                {
                    if (_tool != null) _tool.SetValue(_target, Enum.Parse(t,value), null);
                    if (_toolf != null) _toolf.SetValue(_target, Enum.Parse(t, value));
                }
            }
        }

        public override object EditableObject
        {
            get
            {
                return this;
            }
        }

        public override string Name
        {
            get
            {
                return "Value";
            }
        }
        public override IEnumerable<ExplorableObject> SubObjects
        {
            get
            {
                return null;
            }
        }
        public override System.Windows.Forms.TreeNode Visualize()
        {
            return Visualize(false);
        }
    }
}
